/**
  ******************************************************************************
  * File Name          : ltlx.c
  * Description        : 
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "tskcfg.h"
#include "bsp.h"
#include "ltlx.h"

/* 料塔料线控制任务 */
#define LTLXACT_TSK_PRIORITY                        (tskIDLE_PRIORITY + 3)
#define LTLXACT_STACK_SIZE                          512u
TaskHandle_t xLtLxAct_Thd = NULL;
StaticTask_t xLtLxAct_Tcb;
StackType_t  xLtLxAct_Stk[ LTLXACT_STACK_SIZE ];

/* 料塔料线输入检测任务 */
#define LTLXDTC_TSK_PRIORITY                        (tskIDLE_PRIORITY + 4)
#define LTLXDTC_STACK_SIZE                          512u
TaskHandle_t xLtLxDtc_Thd = NULL;
StaticTask_t xLtLxDtc_Tcb;
StackType_t  xLtLxDtc_Stk[ LTLXDTC_STACK_SIZE ];

/* 料塔料线时间控制任务 */
#define LTLXTIM_TSK_PRIORITY                        (tskIDLE_PRIORITY + 5)
#define LTLXTIM_STACK_SIZE                          512u
TaskHandle_t xLtLxTim_Thd = NULL;
StaticTask_t xLtLxTim_Tcb;
StackType_t  xLtLxTim_Stk[ LTLXTIM_STACK_SIZE ];

/* 输入检测信号量 */
SemaphoreHandle_t LtLx_InDetect_Shd = NULL;
static StaticSemaphore_t ltlx_indetect_area;
 
/* 485 接收消息队列 */
QueueHandle_t LtLx_RxCmd_Qhd = NULL;
static StaticQueue_t ltlx_rxcmd_stru;
static uint8_t       ltlx_rxcmd_area[LTLX_RXCMDQUE_LENGTH * LTLX_RXCMDQUE_ITEMSIZE];

const uint16_t LtLx_SendCmdOn  = LTLX_CMD_ON;
const uint16_t LtLx_SendCmdOff = LTLX_CMD_OFF;

void vLtLxAct_Task( void * pvParameters )
{
	uint16_t rcmd;
	
	Ltlx_BspInit();
	
	LtLx_RxCmd_Qhd = xQueueCreateStatic(LTLX_RXCMDQUE_LENGTH, \
	                                    LTLX_RXCMDQUE_ITEMSIZE, \
	                                    ltlx_rxcmd_area, \
	                                    &ltlx_rxcmd_stru);
  TIM6->CR1 |= TIM_CR1_CEN;
  
	while(1)
	{
		if(xQueueReceive(LtLx_RxCmd_Qhd, &rcmd, 950) == pdTRUE)
		{
			switch(rcmd)
			{
				case LTLX_CMD_ON:   /* 点动开 */
					RELAY01(1);
					vTaskDelay(700);
					RELAY01(0);
					break;
				
				case LTLX_CMD_OFF:  /* 点动关 */
					RELAY02(1);
					vTaskDelay(700);
					RELAY02(0);
					break;
				
				default:
					break;
			}
		}
		
		RELAY01(0);
		RELAY02(0);
		
		vTaskDelay(50);
	}
}

void vLtLxDtc_Task( void * pvParameters )
{
  LtLx_InDetect_Shd = xSemaphoreCreateBinaryStatic(&ltlx_indetect_area);
  
	while(1)
	{
		xSemaphoreTake(LtLx_InDetect_Shd, 1000);
		
		if(IN01() != 0)
		{
			vTaskDelay(100);
			if(IN01() != 0)
			{
				vTaskDelay(100);
				if(IN01() != 0)
				{
					LtLx_State.lxkg_st = 0;  /* 检测关 */
          LtLx_State.lxst = 0;
				}
			}
		}
		
		if(IN01() == 0)
		{
			vTaskDelay(100);
			if(IN01() == 0)
			{
				vTaskDelay(100);
				if(IN01() == 0)
				{
					LtLx_State.lxkg_st = 1;  /* 检测开 */
          LtLx_State.lxst = 1;
				}
			}
		}
	}
}

void vLtLxTim_Task( void * pvParameters )
{
	uint8_t  i;

	while(1)
	{
		if(LtLx_RunPara.lxtmen == 1)
		{
			for(i=0; i<6; i++)
			{
				if((Sys_Runtime.allsec >= LtLx_RunPara.lxtm[i]) && \
           (Sys_Runtime.allsec < (LtLx_RunPara.lxtm[i]+3)) && \
           (LtLx_RunPara.lxtm[i] != 0))
				{
					if(LtLx_RxCmd_Qhd != NULL)
          {
            xQueueSend(LtLx_RxCmd_Qhd, &LtLx_SendCmdOn, 0);
          }
				}
			}
		}
		vTaskDelay(500);
	}
}

void Sys_RunLed(void)
{
  if(LtLx_State.lxkg_st == 1)
  {
    LED1(LED_Off);
    vTaskDelay(150);
    LED1(LED_On);
    vTaskDelay(850);
  }
  else
  {
    LED1(LED_On);
    vTaskDelay(150);
    LED1(LED_Off);
    vTaskDelay(850);
  }
}

void Expio_DevTaskCreat(void)
{
	xLtLxAct_Thd = xTaskCreateStatic(vLtLxAct_Task,
																	"ltxAc",
																	LTLXACT_STACK_SIZE,
																	(void *)1,
																	LTLXACT_TSK_PRIORITY,
																	xLtLxAct_Stk,
																	&xLtLxAct_Tcb);
	
	xLtLxDtc_Thd = xTaskCreateStatic(vLtLxDtc_Task,
																	"ltxDt",
																	LTLXDTC_STACK_SIZE,
																	(void *)1,
																	LTLXDTC_TSK_PRIORITY,
																	xLtLxDtc_Stk,
																	&xLtLxDtc_Tcb);
																	
	xLtLxTim_Thd = xTaskCreateStatic(vLtLxTim_Task,
                                  "ltxTi",
                                  LTLXTIM_STACK_SIZE,
                                  (void *)1,
                                  LTLXTIM_TSK_PRIORITY,
                                  xLtLxTim_Stk,
                                  &xLtLxTim_Tcb);
}
